//
//  YXTextFieldVC.m
//  YXKitOC
//
//  Created by zx on 2020/8/19.
//  Copyright © 2020 张鑫. All rights reserved.
//

#import "YXTextFieldVC.h"
#import "YXKitOC.h"
#import "YXCountDownButton.h"
#import <Masonry/Masonry.h>
#import "YXTableView.h"

@interface YXTextFieldVC ()

@property (nonatomic, strong) NSArray *listArray; /**< 数据源 */
@property (nonatomic, strong) YXTableView *yxTableView; /**< <#描述#> */

@end

@implementation YXTextFieldVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.listArray = @[@{@"text_手机号": @(YXTextFieldType_Phone)},
                       @{@"text_密码": @(YXTextFieldType_Password)},
                       @{@"text_短信验证码": @(YXTextFieldType_MsgCode)},
                       @{@"text_身份证号": @(YXTextFieldType_IDCard)},
                       @{@"text_姓名": @(YXTextFieldType_Name)},
                       @{@"text_里程": @(YXTextFieldType_Mileage_Thousand)},
                       @{@"text_VIN": @(YXTextFieldType_Mileage_VIN)}];
    
    [self.view addSubview:self.yxTableView];
    [self.yxTableView reloadData];
}

#pragma mark - Getter

- (YXTableView *)yxTableView {
    if (!_yxTableView) {
        _yxTableView = [[YXTableView alloc] initWithFrame:YX_SafeFrame()];
        _yxTableView.enableRefresh = NO;
        
        YX_WeakSelf
        [_yxTableView registCells:@[[YXBaseCell class]] returnClass:^Class _Nonnull(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
            return [YXBaseCell class];
        } loadCell:^(UITableView * _Nonnull tableView, id  _Nonnull loadCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
            
            YXBaseCell *cell = loadCell;
            [cell.contentView yx_removeAllSubviews];
            
            NSDictionary *data = cellData;
            NSString *title = data.allKeys.firstObject;
            
            /** 输入框 */
            if ([title hasPrefix:@"text_"]) {
                
                YXTextFieldType type = [data.allValues.firstObject intValue];
                
                YXTextField *textField = [[YXTextField alloc] initWithFrame:CGRectMake(20, 0, cell.contentView.width - 20 * 2, cell.contentView.height)];
                textField.type = type;
                [textField showTitleLabel:^(UILabel * _Nonnull titleLabel) {
                    titleLabel.text = [title substringFromIndex:5];
                    titleLabel.width = 100;
                }];
                [cell.contentView addSubview:textField];
                
                /** 验证码添加倒计时 */
                if (type == YXTextFieldType_MsgCode) {
                    textField.title = @"验证码：";
                    textField.placeholder = @"请输入验证码";
                    
                    [textField showTitleLabel:^(UILabel * _Nonnull titleLabel) {
                        titleLabel.textColor = YX_RedColor();
                    }];
                    
                    [textField showCountDownBtn:^(YXCountDownButton * _Nonnull countDownButton) {
                        
                    }];
                    
                    [textField.countDownButton yx_addEventBlock:^(YXCountDownButton *  _Nonnull sender) {
                        [sender startCountDownWith:60];
                    }];
                    
                    [textField handleTextFieldChangedBlock:^(NSString * _Nonnull text) {
                        NSLog(@"😃😃");
                        NSLog(@"%@ \n\n", text);
                    }];
                }
            }
            
        } returnHeight:^CGFloat(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
            return 50;
        } didSelected:^(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
            
        } loadData:^(NSInteger pageIndex, NSInteger pageSize, YXStatusBlock  _Nonnull statusBlock) {
            statusBlock(nil, weakSelf.listArray);
        }];
    }
    return _yxTableView;
}

@end
